/**
 * Created by yunpeng on 2015/4/3.
 */
var Table = require('../utils/Table').Table;
var Transfer = function(type,carnum,num,picpath,startstation,endstation,starttime,endtime,username,phone,status,createtime,userid,carrierid,price,advicestate,orderno,bidnum){
    this.type = type;
    this.carnum = carnum;
    this.num = num;
    this.picpath = picpath;
    this.startstation = startstation;
    this.endstation = endstation;
    this.starttime = starttime;
    this.endtime = endtime;
    this.username = username;
    this.phone = phone;
    this.status = status;
    this.createtime = createtime;
    this.userid = userid;
    this.carrierid = carrierid;
    this.price = price;
    this.advicestate = advicestate;
    this.orderno = orderno;
    this.bidnum=bidnum;

    Transfer.prototype.save = function(callback){
        new Table('information').insert(this,function(err,result){
            if(err){
                callback(false);
            }else{
                callback(true);
            }
        });
    };

    if(typeof this.getList !='function'){//商家列表
        Transfer.prototype.getList = function(params,page,iscarrier,callback){
            var columns = ' l.id,l.bidnum,l.type,l.carnum,l.num,l.picpath,l.startstation,l.endstation,l.endtime,l.username,l.phone,l.status,date_format(l.createtime,"%Y-%m-%d") as createtime,l.userid,l.carrierid,l.price,l.advicestate ,date_format(l.starttime,"%Y-%m-%d") as starttime,date_format(l.endtime,"%Y-%m-%d") as endtime';
            if(params[0]==0){
                if(iscarrier){
                    var condition1 =' AND l.userid != '+iscarrier+' ';
                }

                if(params[1]&&params[1]!='null'){//开始时间
                    condition1+=" AND l.status in ("+params[1]+") ";
                }
            }else{
                var condition1 =' AND l.userid='+params[0];
                if(params[1]&&params[1]!='null'){//开始时间
                    condition1+=" AND l.status in ("+params[1]+") ";
                }
            }
            new Table('information l').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }
    if(typeof this.carNum !='function'){
        Transfer.prototype.carNum=function(carnum,callback){
            new Table().executeSql("select l.id,l.cbid,l.num,cb.carbrand from carnum l, carbrand cb WHERE l.id in (?) AND l.cbid=cb.id",[carnum],function(err,result){
                if(err){
                    callback(['']);
                }else{
                    callback(result);
                }
            });
        }
    }
    if(typeof this.inforStatus !='function'){
        Transfer.prototype.inforStatus=function(id,callback){
            new Table().executeSql("select * from information l WHERE l.id = (?) ",[id],function(err,result){
                if(err){
                    callback(['']);
                }else{
                    callback(result);
                }
            });
        }
    }
    if(typeof this.queryPrice !='function'){
        Transfer.prototype.queryPrice=function(stations,callback){
            new Table().executeSql(" select l.price from information l WHERE l.startstation = (?) AND l.endstation = (?) AND l.price is not null ",stations,function(err,result){
                if(err){
                    callback(['']);
                }else{
                    callback(result);
                }
            });
        }
    }
    if(typeof this.informationCarnum!='function'){
        Transfer.prototype.informationCarnum=function(id,carnum,num,callback){
            var doc={carnum:''+carnum+'',num:''+num+''};
            new Table('information').updateColumn("id",id,doc,function(err,data){
                if(err){
                    callback(err);
                }else{
                    callback(data);
                }
            });
        }
    }
    if(typeof this.Cancel!='function'){
        Transfer.prototype.Cancel=function(id,callback){
            var doc={status:4};
            new Table('information').updateColumn("id",id,doc,function(err,data){
                if(err){
                    callback(err);
                }else{
                    callback(data);
                }
            });
        }
    }

    if(typeof this.Accept!='function'){
        Transfer.prototype.Accept=function(id,data,callback){
            var doc={status:2,price:data.price,carrierid:data.carrierid};
            new Table().getConnection(function(connection) {
                connection.beginTransaction(function (err) {
                    if (err) { //throw err;
                        callback(false);
                    }
                });
                connection.query("UPDATE information i SET ? WHERE i.id="+id+"", doc, function (err, datas) {
                    if (err) {
                        connection.rollback(function() {
                            //throw err;
                            callback(false);
                        });
                    }
                    var arr={accept:1};
                    connection.query("UPDATE bid b SET ? WHERE b.inforid="+id+" AND b.carrierid="+data.carrierid+"",arr,function(err,data) {
                        if (err) {
                            connection.rollback(function () {
                                //throw err;
                                callback(false);
                            });
                        }
                        connection.commit(function (err) {
                            if (err) {
                                connection.rollback(function () {
                                    //throw err;
                                    callback(false);
                                });
                            } else {
                                connection.release(); //release
                                callback(true);
                            }
                        });
                    })
                });
            });
        }
    }


    if(typeof this.getOrderList !='function'){//商家列表
        Transfer.prototype.getOrderList = function(params,page,callback){
            var columns = ' l.id,l.type,l.carnum,l.num,l.picpath,l.startstation,l.endstation,l.endtime,l.username,l.phone,l.status,date_format(l.createtime,"%Y-%m-%d") as createtime,l.userid,l.carrierid,l.price,l.advicestate ,date_format(l.starttime,"%Y-%m-%d") as starttime,date_format(l.endtime,"%Y-%m-%d") as endtime,l.orderno ';
            var condition1 = '';
            if(params[0]&&params[0]!='null'){//订单编号
                condition1+=" AND l.orderno LIKE '%"+params[0]+"%'";
            }
            if(params[1]&&params[1]!='null'){//开始时间
                condition1+=" AND l.createtime>'"+params[1]+"'";
            }
            if(params[2]&&params[2]!='null'){//结束时间
                condition1+=" AND l.createtime<'"+params[2]+"'";
            }
            if(params[3]&&params[3]!='null'){//商家名称模糊查询
                condition1+=" AND l.phone LIKE '%"+params[3]+"%'";
            }
            if(params[4]&&params[4]!=0){//商家名称模糊查询
                condition1+=" AND l.status = "+params[4]+" ";
            }

            new Table('information l').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }


    if(typeof this.bidList !='function'){//商家列表
        Transfer.prototype.bidList = function(params,page,callback){
            var columns = '  l.id,l.price,l.carrierid,l.accept,u.phone,u.name,u.authstate,date_format(l.createtime,"%Y-%m-%d") as createtime,date_format(l.starttime,"%Y-%m-%d") as starttime,date_format(l.endtime,"%Y-%m-%d") as endtime ';
            var condition1 = ' and l.carrierid = u.id ';

            if(params[0]&&params[0]!='null'){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]&&params[1]!='null'){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]&&params[2]!='null'){//商家名称模糊查询
                condition1+=" AND u.phone LIKE '%"+params[2]+"%'";
            }
            if(params[3]&&params[3]!=2){//商家名称模糊查询
                condition1+=" AND l.accept = "+params[3]+" ";
            }

                condition1+=" AND l.inforid = "+params[4]+" ";


            new Table(' bid l,users u ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }
    if(typeof this.updateBidnum !='function'){
        Transfer.prototype.updateBidnum = function(id,bidnum, callback){
            new Table('information').updateBidnum('id', id,bidnum, function(err,result){
                if(err){
                    callback(500);
                }else{
                    callback(true);
                }
            });
        }
    }

}

exports.Transfer = Transfer;